﻿using System.Security.Cryptography;
using System.Windows;
using System.Windows.Forms;
using System.Text;
using System;

namespace AsymmetricKeysGenerator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string saveto = string.Empty;
            string saveto2 = string.Empty;

            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            DialogResult result = folderBrowserDialog1.ShowDialog();

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                saveto = folderBrowserDialog1.SelectedPath + "\\publickey.xml";
                saveto2 = folderBrowserDialog1.SelectedPath + "\\privatekey.xml";

                var fs = System.IO.File.Create(saveto);
                fs.Close();
                fs.Dispose();

                var fs2 = System.IO.File.Create(saveto2);
                fs2.Close();
                fs2.Dispose();

                //Generate a public/private key pair.
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

                string RSAKeyInfoPublic = RSA.ToXmlString(false);
                string RSAKeyInfoPrivate = RSA.ToXmlString(true);

                Adams.Shared.IOHelper.SaveFileContents(saveto, RSAKeyInfoPublic);
                Adams.Shared.IOHelper.SaveFileContents(saveto2, RSAKeyInfoPrivate);

                System.Windows.MessageBox.Show("Done");
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            string filepath = string.Empty;

            OpenFileDialog fileBrowserDialog1 = new OpenFileDialog();
            DialogResult result = fileBrowserDialog1.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                filepath = fileBrowserDialog1.FileName;

                ClearKey();

                CspParameters cp = new CspParameters();
                cp.KeyContainerName = "ADAMSRSA";
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
                string key = Adams.Shared.IOHelper.GetFileContents(filepath);
                rsa.FromXmlString(key);
                rsa.PersistKeyInCsp = true;
                rsa.Dispose();
            }

            System.Windows.MessageBox.Show("Done");
        }

        private void ClearKey()
        {
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = "ADAMSRSA";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
            rsa.PersistKeyInCsp = false;
            rsa.Clear();
            rsa.Dispose();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            ClearKey();
            System.Windows.MessageBox.Show("Done");
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            string filepath = string.Empty;

            System.Windows.MessageBox.Show("Browse for public key");
            OpenFileDialog fileBrowserDialog1 = new OpenFileDialog();
            DialogResult result = fileBrowserDialog1.ShowDialog();
            filepath = fileBrowserDialog1.FileName;

            //Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            string dataString = "Data to Encrypt";

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToEncrypt = ByteConverter.GetBytes(dataString);
            byte[] encryptedData;
            byte[] decryptedData;

            //Create a new instance of the RSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
            string key = Adams.Shared.IOHelper.GetFileContents(filepath);
            RSAalg.FromXmlString(key);
            

            //Encrypt the byte array and specify no OAEP padding.  
            //OAEP padding is only available on Microsoft Windows XP or
            //later.  
            encryptedData = RSAalg.Encrypt(dataToEncrypt, false);
            RSAalg.Dispose();

            string en = ByteConverter.GetString(encryptedData);

            CspParameters cp = new CspParameters();
            cp.KeyContainerName = "ADAMSRSA";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);

            //Pass the data to ENCRYPT and boolean flag specifying 
            //no OAEP padding.
            decryptedData = rsa.Decrypt(encryptedData, false);
            rsa.Dispose();

            string de = ByteConverter.GetString(decryptedData);

            if (string.Compare(de, dataString, false) == 0)
            {
                System.Windows.MessageBox.Show("Successul");
            }
            else
            {
                System.Windows.MessageBox.Show("Failed");
            }
        }
    }
}
